<template>
  <div>
    <el-form ref="form" :rules="rules" :model="sysDictType" label-width="100px" size="small">
      <el-form-item label="字典名称" prop="dictName">
        <el-input v-model="sysDictType.dictName" placeholder="请输入字典名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="字典类型" prop="dictType">
        <el-input v-model="sysDictType.dictType" placeholder="请输入字典类型" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="sysDictType.remark" placeholder="请输入备注" clearable></el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="sysDictType.status">
          <el-radio v-for="item in statusOptions" :key="item.dictValue"  :label="item.dictValue">{{item.dictLabel}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  size="small" @click="update">修改</el-button>
        <el-button type="warning" size="small" @click="sysDictType={status: 0}">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import sysDictTypeApi from "@/api/system/sysDictType"
export default {
  name: "sys-dict-type-add.vue",
  //props传参
  props:{
    activeId:{
      type: Number,
      default:null
    }
  },
  data(){
    return{
      //要添加的表单数据
      sysDictType:{
        dictName:undefined,
        dictType:undefined,
        remark:undefined,
        status:0
      },
      //表单验证
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ]
      },
      //数据状态的字典数据
      statusOptions:[]
    }
  },
  //监听父组件里面的activeId
  watch:{
    activeId:{
      immediate:true,
      handler:function (newVal,oldVal){
        this.getById(newVal);
      }
    }
  },
  created() {
    this.getDataByType("sys_status_type").then(res=>{
      this.statusOptions=res.data;
    })
  },
  methods:{
    //根据ID查询字典类型
    getById(dictId){
      sysDictTypeApi.getById(dictId).then(res=>{
        this.sysDictType=res.data;
      })
    },
    //更新
    update(){
      //触发表单验证
      this.$refs["form"].validate(valid=>{
        if(valid){
          sysDictTypeApi.update(this.sysDictType).then(res=>{
            this.$message.success(res.msg)
            this.$emit("after")
            this.$emit("close")
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
